class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        int hash[26] = {0};
        for(int i = 0; i < croakOfFrogs.size(); i++)
        {
            if(croakOfFrogs[i] == 'c')
            {
                if(hash['k' - 'a'] >= 1)    // 存在
                    hash['k' - 'a']--;

                hash['c' - 'a']++;
            }
            else if(croakOfFrogs[i] == 'r')
            {
                if(hash['c' - 'a'] >= 1)    // 存在
                {
                    hash['c' - 'a']--;
                    hash['r' - 'a']++;
                }
                else
                {
                    return -1;
                }
            }
            else if(croakOfFrogs[i] == 'o')
            {
                if(hash['r' - 'a'] >= 1)    // 存在
                {
                    hash['r' - 'a']--;
                    hash['o' - 'a']++;
                }
                else
                {
                    return -1;
                }
            }
            else if(croakOfFrogs[i] == 'a')
            {
                if(hash['o' - 'a'] >= 1)    // 存在
                {
                    hash['o' - 'a']--;
                    hash['a' - 'a']++;
                }
                else
                {
                    return -1;
                }
            }
            else if(croakOfFrogs[i] == 'k')
            {
                if(hash['a' - 'a'] >= 1)    // 存在
                {
                    hash['a' - 'a']--;
                    hash['k' - 'a']++;
                }
                else
                {
                    return -1;
                }
            }
        }
        if(hash['c' - 'a'] == 0
            && hash['r' - 'a'] == 0
            && hash['o' - 'a'] == 0
            && hash['a' - 'a'] == 0
            && hash['k' - 'k'] == 0)
            return hash['k' - 'a'] == 0 ? -1 : hash['k' - 'a'];
        else
            return -1;
    }
};

// 高级优化
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string t = "croak";
        int n = t.size();
        vector<int> hash(n);    // 用数组来模拟哈希表

        unordered_map<char, int> index; // [x, x这个字符对应的下标]
        for(int i = 0; i < n; i++)
            index[t[i]] = i;

        for(const auto& ch : croakOfFrogs)
        {
            if(ch == 'c')
            {
                if(hash[n - 1] != 0) hash[n - 1]--;
                hash[0]++;
            }
            else
            {
                int i = index[ch];
                if(hash[i - 1] == 0) return -1;
                hash[i - 1]--;
                hash[i]++;
            }
        }

        for(int i = 0; i < n - 1; i++)
            if(hash[i] != 0)
                return -1;

        return hash[n - 1];
    }
};